<template>
  <div>
    <div style="height: 342px;overflow-y: auto;overflow-x: hidden">
      <el-tree
        :data="treeData"
        ref="objectTree"
        :props="treeProps"
        node-key="id"
        icon-class="ivu-icon-ios-arrow-forward"
        :show-checkbox="true"
        @check-change="onCheckChange"
        :check-strictly="true"
      ></el-tree>
    </div>
  </div>
</template>
<script>
import locales from "./locales.js";
import { queryTreeDimensionData, saveAssignedDimensionData } from "@/api/dimensionApi.js";
export default {
  name: "AssignTreeDimension",
  mixins: [locales],
  props: {
    roleId: {
      type: String,
      default() {
        return "";
      }
    },
    tenantCode: {
      type: String,
      default() {
        return "";
      }
    },
    dimensionId: {
      type: String,
      default() {
        return "";
      }
    },
    dimensionCode: {
      type: String,
      default() {
        return "";
      }
    }
  },
  data() {
    return {
      treeData: [],
      treeProps: {
        children: "children",
        label: "text"
      },
      param: {
        roleId: this.roleId,
        tenantCode: this.tenantCode,
        dimensionId: this.dimensionId,
        dimensionCode: this.dimensionCode
      },
      checkedList: [],
      isCheckChange: false
    };
  },
  created: function() {
    this.init();
  },
  methods: {
    init() {
      // 初始化树数据
      queryTreeDimensionData(this.param)
        .then(response => {
          this.treeData = response.data || [];
          // 设置选中节点
          this.initCheckedNode(response.data || []);
          this.$refs["objectTree"].setCheckedKeys(this.checkedList);
        })
        .catch(response => {
          console.log(1, response);
          this.$message.error(response.msg);
        });
    },
    initCheckedNode(children) {
      if (!Array.isArray(children)) return;
      children.forEach(node => {
        this.initCheckedNode(node.children);
        // 勾选的节点
        if (node.selected == 1) {
          this.checkedList.push(node.id);
        }
      });
      return;
    },
    save() {
      // 保存已分配维度数据
      let checkedNodes = this.$refs["objectTree"].getCheckedNodes();
      let assignedData = [];
      checkedNodes.forEach(item => {
        assignedData.push({
          dimensionCode: this.dimensionCode,
          dimensionDataCode: item.value,
          dimensionDataId: item.id,
          dimensionDataName: item.text,
          roleId: this.roleId
        });
      });

      let param = {
        roleId: this.roleId,
        dimensionCode: this.dimensionCode,
        roleDataRelVos: assignedData
      };

      // $store
      // .dispatch("SaveAssignedDimensionData", param)
      saveAssignedDimensionData(param)
        .then(response => {
          this.isCheckChange = false;
          this.$message.success({
            content: this.$t("module.dimension.warning.updateSuccessfully"),
            duration: 3
          });
        })
        .catch(response => {
          this.$message.error(response.msg);
        });
    },
    onCheckChange() {
      this.isCheckChange = true;
    },
    isChange() {
      return this.isCheckChange;
    }
  },
  watch: {},
  directives: {}
};
</script>
